﻿using AutoMapper;
using IOA.MES.DataAccess.Entity;
using IOA.MES.DataAccess.VModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IOA.MES.Common;

namespace IOA.MES.Business
{
    /// <summary>
    /// 设备参数配置（错误代码：125001）
    /// </summary>
    public class DeviceParamMgr
    {
        private MESContext mesDb = new MESContext("MES");

        /// <summary>
        /// 编辑
        /// </summary>
        public VDeviceParam Edit(VDeviceParam model)
        {
            if (model.PkId <= 0)
            {
                model.CreateTime = DateTime.Now;
                model.ModifyBy = 0;
                var entity = Mapper.Map<DeviceParam>(model);
                mesDb.DeviceParams.Add(entity);
                mesDb.SaveChanges();

                model = Mapper.Map<VDeviceParam>(entity);
            }
            else
            {
                var entity = mesDb.DeviceParams.FirstOrDefault(_ => _.PkId == model.PkId && _.Valid);
                if (entity == null)
                {
                    throw new MultiLanBizException(125001);// "待编辑的记录不存在或已被删除，无法编辑");
                }
                entity.OrderNo = model.OrderNo;
                entity.StandardMin = model.StandardMin;
                entity.StandardMax = model.StandardMax;
                entity.DeviceNo = model.DeviceNo;
                entity.DeviceType = model.DeviceType;
                entity.ParamName = model.ParamName;
                entity.ModifyBy = model.ModifyBy;
                entity.ModifyTime = DateTime.Now;
                mesDb.SaveChanges();

                model = Mapper.Map<VDeviceParam>(entity);
            }
            return model;
        }

        /// <summary>
        /// 查询
        /// </summary>
        public VDeviceParam Get(int pkId, string orderNo)
        {
            var entity = mesDb.DeviceParams.FirstOrDefault(_ => _.PkId == pkId && _.Valid && _.OrderNo.Equals(orderNo));
            if (entity == null)
            {
                entity = new DeviceParam();
            }
            return Mapper.Map<VDeviceParam>(entity);
        }

        /// <summary>
        /// 搜索
        /// </summary>
        public List<VDeviceParam> Query(int pageIndex, int pageSize, out int total, string deviceType, string deviceNo, string paramName)
        {
            var query = mesDb.DeviceParams.Where(_ => _.Valid);
            if (!string.IsNullOrWhiteSpace(deviceType))
            {
                query = query.Where(_ => _.DeviceType.Equals(deviceType));
            }
            if (!string.IsNullOrWhiteSpace(deviceNo))
            {
                query = query.Where(_ => _.DeviceNo.Equals(deviceNo));
            }
            if (!string.IsNullOrWhiteSpace(paramName))
            {
                query = query.Where(_ => _.ParamName.Equals(paramName));
            }

            total = query.Count();
            var entities = query.OrderByDescending(_ => _.PkId).Skip((pageIndex - 1) * pageSize).Take(pageSize).OrderByDescending(_ => _.PkId).ToList();

            return Mapper.Map<List<VDeviceParam>>(entities);
        }


        /// <summary>
        /// 删除
        /// </summary>
        public void Delete(VDeviceParam model)
        {
            var entity = mesDb.DeviceParams.FirstOrDefault(_ => _.PkId == model.PkId && _.Valid);
            if (entity == null)
            {
                throw new MultiLanBizException(125002);// "数据不存在或已被删除，无法重复删除");
            }
            entity.Valid = false;
            entity.ModifyBy = model.ModifyBy;
            entity.ModifyTime = DateTime.Now;
            mesDb.SaveChanges();
        }
    }
}
